<template>
    <!-- 导航栏 -->
    <van-nav-bar
      title="新增收货地址"
      left-text="返回"
      left-arrow
      @click-left="back"
    />

    <!-- 新增表单 -->
    <van-address-edit
      :area-list="areaList"
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      :address-info="AddressInfo"
      @save="save"
    />
</template>


<script>
  // 引入地区初始化数据
  import { areaList } from '@vant/area-data';
  export default {
    name:'AddressAdd',
    data()
    {
      return {
        areaList,
        LoginUser: this.$cookies.get('LoginUser'),
      }
    },
    computed:{
      AddressInfo()
      {
        var name = this.LoginUser.nickname

        var areaCode = ''

        if(this.LoginUser.province)
        {
          areaCode = this.LoginUser.province.toString()
        }

        if(this.LoginUser.city)
        {
          areaCode = this.LoginUser.city.toString()
        }

        if(this.LoginUser.district)
        {
          areaCode = this.LoginUser.district.toString()
        }

        return {
          name,
          areaCode
        }
      }
    },
    methods:{
      back()
      {
        this.$router.go(-1)
      },
      //保存地址
      async save(content)
      {
        
        // return false;
        //组装数据
        var data = {
          consignee: content.name,
          mobile: content.tel,
          code: content.areaCode,
          address: content.addressDetail,
          status: content.isDefault ? 1 : 0,
          userid: this.LoginUser.id
        }

        console.log(data);
        //调用接口
        var result = await this.$api.AddressAdd(data)
        console.log(result);
        if(result.code == 1)
        {
          //成功
          this.$notify({
            type:'success',
            message:result.msg,
            duration:1000,
            onClose: () => {
              this.$router.go(-1)
            }
          })
        }else
        {
          //失败
          this.$notify({
            type:'danger',
            message:result.msg,
            duration:1000,
          })
        }
      }
      
    }
  }
</script>